package com.company.Thread;

import java.util.Random;
import java.util.concurrent.*;

import cn.hutool.core.date.DateTime;

public class ThreadPoolMain {
    public static void main(String[] args) {
        // IntStream.range(0, 20).parallel().forEach(item -> {
        // try {
        // System.out.println(StrUtil.format("线程{}开始执行{}", item, DateUtil.now()));
        // Thread.sleep(500L * item);
        // System.out.println(StrUtil.format("线程{}执行完成{}", item, DateUtil.now()));
        // } catch (InterruptedException e) {
        // e.printStackTrace();
        // }
        // });
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() - startTime);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

        ThreadPoolExecutor executor =
            new ThreadPoolExecutor(10, 15, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5));
        Future<Integer>[] futures = new Future[3];
        for (int i = 0; i < 10; i++) {
            futures[i] = executor.submit(() -> {
                System.out.println("await|" + Thread.currentThread().getName());
                cyclicBarrier.await();
                Random rand = new Random();
                int n = rand.nextInt(100);
                int result = 0;
                for (int j = 0; j < n; j++) {
                    result += j;
                }
                Thread.sleep(1000L);
                System.out.println(DateTime.now());
                System.out.println(result + "|" + Thread.currentThread().getName());
                return result;
            });
        }
    }
}
